﻿using System;
using System.Xml;

namespace ZYCommon
{
   

    public class XMLCommon
    {
        public static void ClearChildNode(XmlNode myNode)
        {
            if (myNode != null)
            {
                while (myNode.FirstChild != null)
                {
                    myNode.RemoveChild(myNode.FirstChild);
                }
            }
        }

        public static XmlElement CreateChildElement(XmlElement RootElement, string strName, bool bolCreate)
        {
            XmlElement element2=null;
            if (((RootElement == null) || (strName == null)) || (strName.Trim().Length == 0))
            {
                return null;
            }
            try
            {
                strName = strName.Trim();
                foreach (XmlNode node in RootElement.ChildNodes)
                {
                    if (node.Name == strName)
                    {
                        return (XmlElement) node;
                    }
                }
                if (!bolCreate)
                {
                    return null;
                }
                XmlElement newChild = RootElement.OwnerDocument.CreateElement(strName);
                RootElement.AppendChild(newChild);
                element2 = newChild;
            }
            catch
            {
            }
            return element2;
        }

        public static XmlElement GetElementByAttribute(XmlElement rootElement, string strName, string strAttrName, string strAttrValue, bool bolCreate)
        {
            XmlElement newChild = null;
            if (((rootElement != null) && (strName != null)) && ((strAttrName != null) && (strAttrValue != null)))
            {
                foreach (XmlNode node in rootElement.ChildNodes)
                {
                    newChild = node as XmlElement;
                    if (((newChild != null) && (newChild.Name == strName)) && (newChild.HasAttribute(strAttrName) && (newChild.GetAttribute(strAttrName) == strAttrValue)))
                    {
                        return newChild;
                    }
                }
                if (bolCreate)
                {
                    newChild = rootElement.OwnerDocument.CreateElement(strName);
                    newChild.SetAttribute(strAttrName, strAttrValue);
                    rootElement.AppendChild(newChild);
                    return newChild;
                }
            }
            return null;
        }

        public static XmlElement GetElementByName(XmlElement rootNode, string strName, bool Deep)
        {
            if (((rootNode != null) && (strName != null)) && (strName.Trim().Length != 0))
            {
                strName = strName.Trim();
                foreach (XmlNode node in rootNode.ChildNodes)
                {
                    if (node.Name == strName)
                    {
                        return (XmlElement) node;
                    }
                    if ((Deep && (node is XmlElement)) && (node.ChildNodes.Count > 0))
                    {
                        XmlElement element = GetElementByName((XmlElement) node, strName, true);
                        if (element != null)
                        {
                            return element;
                        }
                    }
                }
            }
            return null;
        }

        public static string GetElementValue(XmlElement RootElement, string strName)
        {
            foreach (XmlNode node in RootElement.ChildNodes)
            {
                if (node.Name == strName)
                {
                    return node.InnerText;
                }
            }
            return null;
        }

        public static XmlNode GetXMLNodeByPath(XmlNode rootNode, string strPath)
        {
            if ((strPath != null) && (rootNode != null))
            {
                int index = strPath.IndexOf(".");
                string str = null;
                if (index > 0)
                {
                    str = strPath.Substring(0, index);
                    strPath = strPath.Substring(index + 1);
                }
                else
                {
                    str = strPath;
                    strPath = null;
                }
                foreach (XmlNode node in rootNode.ChildNodes)
                {
                    if (node.Name == str)
                    {
                        if (strPath == null)
                        {
                            return node;
                        }
                        return GetXMLNodeByPath(node, strPath);
                    }
                }
            }
            return null;
        }

        public static bool SetElementValue(XmlElement RootElement, string strName, string strValue)
        {
            XmlElement newChild = null;
            foreach (XmlNode node in RootElement.ChildNodes)
            {
                if (node.Name == strName)
                {
                    newChild = (XmlElement) node;
                    break;
                }
            }
            if (newChild == null)
            {
                newChild = RootElement.OwnerDocument.CreateElement(strName);
                RootElement.AppendChild(newChild);
            }
            newChild.InnerText = strValue;
            return true;
        }
    }
}

